package com.handler;

import com.entity.Response;
import com.enumeration.ResponseEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLSyntaxErrorException;


/**
 * @author Liang
 * @file ValidAdvice.java
 * @brief
 * @email 649355204@qq.com
 * @time 2019/9/24 17:06
 */
@RestControllerAdvice
@Log4j2
public class MyExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @CrossOrigin
    @ExceptionHandler(UnauthenticatedException.class)
    @ResponseBody
    public Response handleUnauthenticatedException(Exception ex) {
        Response response = new Response();
        logger.debug("UnauthenticatedException");
        logger.debug(ex.getMessage());
//        logger.debug(ex.getCause());
        logger.debug(ex.getLocalizedMessage());
//        logger.debug(ex.getClass());
//        logger.debug(ex.getStackTrace());
        return response.setFailed()
                .setErrCode(ResponseEnum.UNAUTHENTICATED_ERROR.getCode())
                .setErrMsg(ResponseEnum.UNAUTHENTICATED_ERROR.getMsg());
    }

    @CrossOrigin
    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public Response handleAuthenticationException(Exception ex) {
        logger.debug("AuthenticationException");
        Response response = new Response();
        return response.setFailed()
                .setErrCode(ResponseEnum.UNAUTHENTICATED_ERROR.getCode())
                .setErrMsg(ResponseEnum.UNAUTHENTICATED_ERROR.getMsg());
    }

    @CrossOrigin
    @ExceptionHandler(ShiroException.class)
    @ResponseBody
    public Response handleShiroException(Exception ex) {
        logger.debug("ShiroException");
        Response response = new Response();
        return response.setFailed()
                .setErrCode(ResponseEnum.PERMISSION_DENIED.getCode())
                .setErrMsg(ResponseEnum.PERMISSION_DENIED.getMsg());
    }

    @CrossOrigin
    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public Response handleShirUnauthorizedException(Exception ex) {
        Response response = new Response();
        logger.debug("UnauthorizedException");
        logger.debug(ex.getMessage());
        return response.setFailed()
                .setErrMsg(ResponseEnum.PERMISSION_DENIED.getMsg())
                .setErrCode(ResponseEnum.PERMISSION_DENIED.getCode());
    }

    @ExceptionHandler(value = DuplicateKeyException.class)
    public Response OracleException(Exception e) {
        Response response = new Response();
        response.setFailed().setErrMsg("数据已存在！");
        return response;
    }

    @ExceptionHandler(value = org.mybatis.spring.MyBatisSystemException.class)
    public Response MyBatisSystemException(Exception ex) {
        Response response = new Response();
        logger.debug("parameterProblem");
        logger.debug(ex.getMessage());
        return response.setFailed()
                .setParamError();
    }

    @ExceptionHandler(value = SQLSyntaxErrorException.class)
    public Response SQLSyntaxErrorException(Exception ex) {
        Response response = new Response();
        logger.debug("parameterProblem");
        logger.debug(ex.getMessage());
        return response.setFailed()
                .setParamError();

    }
//    @ExceptionHandler(value = NullPointerException.class)
//    public Response NullPointerException(Exception e) {
//        Response response = new Response();
//        response.setFailed().setErrMsg("请求参数为空或返回数据为空").setErrCode(500);
//        return response;
//    }
//    @ExceptionHandler(value = Exception.class)
//    public Response UniException(Exception e) {
//        Response response = new Response();
//        System.out.println("cuowu"+e);
//        logger.debug("错误："+e.getMessage());
//        response.setFailed().setErrMsg("服务异常！").setErrCode(500);
//        return response;
//    }
}
